/*
 * @Author: your name
 * @Date: 2021-02-02 09:56:45
 * @LastEditTime: 2021-02-26 22:11:17
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Editm
 * @FilePath: \myproject\src\router\index.js
 */
// const Login = () => import('../pages/Login')模板
// start
import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
const Login = () => import('@/views/Login/index.vue')
const Layout = () => import('@/views/Layout/index.vue')
const Home = () => import('@/views/Home/index.vue')
const User = () => import('@/views/User/index.vue')
const SearchIndex = () => import('@/views/Search/index.vue')
const SearchResult = () => import('@/views/Search/SearchResult.vue')
const ArticleDetail = () => import('@/views/ArticleDetail/index.vue')
const EditUser = () => import('@/views/User/components/editUser.vue')
const ChatMachion = () => import('@/views/Chat/index.vue')
// end
// import Vue from 'vue'
// import VueRouter from 'vue-router'
// import Login from '@/views/Login/index.vue'
// import Layout from '@/views/Layout/index.vue'
// import Home from '@/views/Home/index.vue'
// import User from '@/views/User/index.vue'
// import SearchIndex from '@/views/Search/index.vue'
// import SearchResult from '@/views/Search/SearchResult.vue'
// import ArticleDetail from '@/views/ArticleDetail/index.vue'
// import EditUser from '@/views/User/components/editUser.vue'
// import ChatMachion from '@/views/Chat/index.vue'
// import store from '@/store'
// import { Store } from 'vuex'
// import Home from '../views/Home.vue'
Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/',
    name: 'Layout',
    component: Layout,
    children: [
      {
        path: '',
        name: 'home',
        component: Home
      }, {
        path: '/user',
        name: 'User',
        component: User
      }
    ]
  },
  {
    path: '/SearchIndex',
    name: 'SearchIndex',
    component: SearchIndex
  },
  {
    path: '/SearchResult',
    name: 'SearchResult',
    component: SearchResult
  },
  {
    path: '/articleDetail',
    name: 'ArticleDetail',
    component: ArticleDetail
  },
  {
    path: '/editUser',
    name: 'EditUser',
    component: EditUser
  }, {
    path: '/chatMachion',
    name: 'ChatMachion',
    component: ChatMachion
  }

]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  const permissionList = ['/user']
  if (permissionList.includes(to.path)) {
    if (store.state.tokenInfo && store.state.tokenInfo.token) {
      next()
    } else {
      next({
        path: '/login',
        query: {
          backto: to.fullPath
        }
      })
    }
  } else {
    next()
  }
})

export default router
